<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class broadcast_model extends MY_Model
{
    public function getBroadcastList($per_page, $limit){
        
        $where = " 1=1";
        if(!empty($data['starttime'])){
            $where .= " AND p.createtime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $data['endtime'] = $data['endtime']+3600*24;
            $where .= " AND p.createtime <= {$data['endtime']}";
        }
        if(!empty($data['content'])){
            $where .= " AND p.content like '%{$data['content']}%'";
        }
 

        $limit = " limit {$per_page},{$limit}";

        $sql = "SELECT * FROM tr_sys_broadcast WHERE  {$where}  ORDER BY publishtime DESC {$limit} ";

        $query = $this->db->query($sql);
        $result =$query->result_array();
        return $result;
    }


    public function getBroadcastCnt($data = array()){
        $where = " 1=1 ";
        if(!empty($data['starttime'])){
            $where .= " AND p.createtime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $data['endtime'] = $data['endtime']+3600*24;
            $where .= " AND p.createtime <= {$data['endtime']}";
        }
        if(!empty($data['content'])){
            $where .= " AND p.content like '%{$data['content']}%'";
        }
        $sql = "SELECT * FROM tr_sys_broadcast  WHERE {$where}  ";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }
    public function getMyBroadcastList(){
        $listSql = "SELECT * FROM tr_sys_broadcast_user_map where admin_userid = {$_SESSION['adminUserInfo']['id']} ORDER BY createtime desc";
        $listQuery = $this->db->query($listSql);
        $listResult = $listQuery->result_array();
        if($listResult){
            foreach ($listResult as $key => $value) {
                $broadcastSql = "SELECT * FROM tr_sys_broadcast where id={$value['broadcastid']}";
                $broadcastQuery = $this->db->query($broadcastSql);
                $listResult[$key]['broadcastInfo'] = $broadcastQuery->row_array();
            }
        }
        return $listResult;
    }


     //编辑
    public function editBroadcast($id,$data = array()){
        if($id){
            $this->db->where("id",$id);
            $res = $this->db->update("tr_sys_broadcast",$data);
        }else{
            $res = $this->db->insert("tr_sys_broadcast",$data);
        }
        return $res;
    }

    public function delBroadcast($id){
        $this->db->where("id",$id);
        $data = array(
            'isDel'=>1
        );
        $res = $this->db->update("tr_sys_broadcast",$data);
        return $res;
    }

     //根据id获取广告信息
    public function getBroadcastInfobyID($id){
        $this->db->where("id",$id);
        $query = $this->db->get("tr_sys_broadcast");
        return $query->row_array();
    }

    public function do_publish($data){
        $sql = "INSERT INTO tr_sys_broadcast_user_map(admin_userid,broadcastid,createtime) values $data";
        $result = $this->db->query($sql);
        return $this->db->insert_id();
    }


    public function getBroadcastDetail($id = 0){
        $broadcastSql = "SELECT * FROM tr_sys_broadcast where id={$id}";
        $broadcastQuery = $this->db->query($broadcastSql);
        $broadcastInfo = $broadcastQuery->row_array();
        return $broadcastInfo;
    }

    public function setRead($id){
        $this->db->where(array("broadcastid"=>$id,"admin_userid"=>$_SESSION['adminUserInfo']['id']));
        $res = $this->db->update("tr_sys_broadcast_user_map",array("readtime"=>time()));
        return $res;
    }

}


